Option Compare Text

Imports System.Data.SqlClient
Imports System.Threading
Imports System.Text

Friend Class DBScanDatabase
    Inherits DBBase

    Private moDatabase As SQLAutoDocLib.Database

    Private mlSessionID As Long = Session.NODATA
    Private mlScanTypeID As ScanSchedule.Type = ScanSchedule.NODATA

    Public Sub New( _
        ByVal oDatabase As SQLAutoDocLib.Database)

        moDatabase = oDatabase
    End Sub

    Public Sub New( _
        ByVal oDatabase As SQLAutoDocLib.Database, _
        ByVal lSessionID As Long)

        moDatabase = oDatabase
        mlSessionID = lSessionID
    End Sub

    Public Sub New( _
        ByVal oDatabase As SQLAutoDocLib.Database, _
        ByVal lSessionID As Long, _
        ByVal lScanTypeID As ScanSchedule.Type)

        moDatabase = oDatabase
        mlSessionID = lSessionID
        mlScanTypeID = lScanTypeID
    End Sub

    Public Sub Scan()
        Select Case mlScanTypeID
            Case ScanSchedule.Type.BlockScan
                RecordBlocks(mlSessionID)

            Case ScanSchedule.Type.ConnectionScan
                RecordConnections(mlSessionID)

            Case ScanSchedule.Type.SizeScan
                RecordSize(mlSessionID)

            Case ScanSchedule.Type.WaitStateScan
                RecordWaitStates(mlSessionID)

            Case ScanSchedule.Type.SchemaScan
                Database_ItemScanStart(mlSessionID)

                Dim oServer As New Server(moDatabase.ServerID)
                oServer.Load()

                Dim oLoginScanner As New DBScanLogin(oServer, moDatabase)
                AddHandler oLoginScanner.ItemScanStart, AddressOf Login_ItemScanStart
                AddHandler oLoginScanner.ItemScanComplete, AddressOf Login_ItemScanComplete
                Dim bLoginsModified As Boolean = oLoginScanner.Scan(mlSessionID)
                RemoveHandler oLoginScanner.ItemScanStart, AddressOf Login_ItemScanStart
                RemoveHandler oLoginScanner.ItemScanComplete, AddressOf Login_ItemScanComplete

                Dim oTableScanner As New DBScanTable(oServer, moDatabase)
                AddHandler oTableScanner.ItemScanStart, AddressOf Table_ItemScanStart
                AddHandler oTableScanner.ItemScanComplete, AddressOf Table_ItemScanComplete
                Dim bTablesModified As Boolean = oTableScanner.Scan(mlSessionID)
                RemoveHandler oTableScanner.ItemScanStart, AddressOf Table_ItemScanStart
                RemoveHandler oTableScanner.ItemScanComplete, AddressOf Table_ItemScanComplete

                Dim oViewScanner As New DBScanView(oServer, moDatabase)
                AddHandler oViewScanner.ItemScanStart, AddressOf View_ItemScanStart
                AddHandler oViewScanner.ItemScanComplete, AddressOf View_ItemScanComplete
                Dim bViewsModified As Boolean = oViewScanner.Scan(mlSessionID)
                RemoveHandler oViewScanner.ItemScanStart, AddressOf View_ItemScanStart
                RemoveHandler oViewScanner.ItemScanComplete, AddressOf View_ItemScanComplete

                Dim oSProcScanner As New DBScanProcedure(oServer, moDatabase)
                AddHandler oSProcScanner.ItemScanStart, AddressOf SProc_ItemScanStart
                AddHandler oSProcScanner.ItemScanComplete, AddressOf SProc_ItemScanComplete
                Dim bProcsModified As Boolean = oSProcScanner.Scan(mlSessionID)
                RemoveHandler oSProcScanner.ItemScanStart, AddressOf SProc_ItemScanStart
                RemoveHandler oSProcScanner.ItemScanComplete, AddressOf SProc_ItemScanComplete

                Dim oFunctionScanner As New DBScanFunction(oServer, moDatabase)
                AddHandler oFunctionScanner.ItemScanStart, AddressOf Function_ItemScanStart
                AddHandler oFunctionScanner.ItemScanComplete, AddressOf Function_ItemScanComplete
                Dim bFunctionsModified As Boolean = oFunctionScanner.Scan(mlSessionID)
                RemoveHandler oFunctionScanner.ItemScanStart, AddressOf Function_ItemScanStart
                RemoveHandler oFunctionScanner.ItemScanComplete, AddressOf Function_ItemScanComplete

                If (bLoginsModified _
                    OrElse bTablesModified _
                    OrElse bViewsModified _
                    OrElse bProcsModified _
                    OrElse bFunctionsModified) _
                    AndAlso moDatabase.RevID > 0 Then

                    moDatabase.Rev()
                End If

                RecordExtendedProperties(mlSessionID)

                RemoveExpiredData(mlSessionID)

                Database_ItemScanComplete(mlSessionID)

        End Select
    End Sub

    Private Sub RecordSize( _
                ByVal lSessionID As Long)

        Size_ItemScanStart(mlSessionID)

        Try
            Dim oTableFactory As New SQLAutoDocLib.Table_Factory
            For Each oTable As SQLAutoDocLib.Table In oTableFactory.GetAllTablesForDatabase(moDatabase.ID)
                RecordSizeForSingleTable(lSessionID, oTable)
            Next
        Catch oEX As Exception
            Dim oFactory As New SQLAutoDocLib.TableSize_Factory
            oFactory.RemoveSessionData(mlSessionID)

            Throw oEX
        End Try

        Size_ItemScanComplete(mlSessionID)
    End Sub

    Private Sub RecordSizeForSingleTable( _
                        ByVal lSessionID As Long, _
                        ByVal oTable As SQLAutoDocLib.Table)

        Dim oOwner As New SQLAutoDocLib.Login(oTable.OwnerID)
        oOwner.Load()

        Dim sSQL As String = "exec dbo.sp_spaceused '" & oOwner.Login & ".[" & oTable.Name.Replace("'", "''") & "]'"

        Dim oCon As New SqlConnection(moDatabase.DecryptedConnectionString)
        oCon.Open()

        Dim oCMD As New SqlCommand(sSQL, oCon)
        oCMD.CommandType = CommandType.Text

        Dim oDT As New SqlDataAdapter(oCMD)

        Dim oData As New DataTable

        Dim iTry As Integer = 0

        Do While iTry < 3
            Try
                oDT.Fill(oData)

                Exit Do

            Catch oEX As SqlException
                If oEX.Message.Contains("does not exist in database") Then
                    '---this table has been deleted.
                    Exit Do
                Else
                    iTry = iTry + 1

                    If iTry = 3 Then
                        Throw oEX
                    End If
                End If
            End Try
        Loop

        oDT.Dispose()
        oCMD.Dispose()
        oCon.Close()
        oCon.Dispose()

        oDT.Dispose()

        If oData.Rows.Count > 0 Then
            Dim oTableSize As New TableSize
            oTableSize.TableID = oTable.ID
            oTableSize.TableRevID = oTable.RevID
            oTableSize.SessionID = lSessionID
            oTableSize.RowCount = oData.Rows(0).Item("rows")
            oTableSize.Reserved = StripAwayKB(oData.Rows(0).Item("reserved"))
            oTableSize.Data = StripAwayKB(oData.Rows(0).Item("data"))
            oTableSize.Index_Size = StripAwayKB(oData.Rows(0).Item("index_size"))
            oTableSize.Unused = StripAwayKB(oData.Rows(0).Item("unused"))

            oTableSize.Save()
        End If
    End Sub

    Private Function StripAwayKB(ByVal sValue As String) As Long
        Return CLng(sValue.Split(" ")(0))
    End Function

    Private Sub RecordBlocks(ByVal lSessionID As Long)
        Dim sSQL As New Stringbuilder("select")
        sSQL.Append("     a.[spid] as [blocked_spid],")
        sSQL.Append("     a.[status] as [blocked_status],")
        sSQL.Append("     a.[hostname] as [blocked_hostname],")
        sSQL.Append("     a.[program_name] as [blocked_program_name],")
        sSQL.Append("     a.[cmd] as [blocked_command],")
        sSQL.Append("     convert(sysname, rtrim(a.[loginame]))")
        sSQL.Append("        as [blocked_loginname],")

        sSQL.Append("     b.[spid] as [blocking_spid],")
        sSQL.Append("     b.[status] as [blocking_status],")
        sSQL.Append("     b.[hostname] as [blocking_hostname],")
        sSQL.Append("     b.[program_name] as [blocking_program_name],")
        sSQL.Append("     b.[cmd] as [blocking_command],")
        sSQL.Append("     convert(sysname, rtrim(b.[loginame]))")
        sSQL.Append("        as [blocking_loginname]")
        sSQL.Append(" from ")
        sSQL.Append("	    master.dbo.sysprocesses a with (nolock)")
        sSQL.Append("		    left outer join master.dbo.sysprocesses b with (nolock) on")
        sSQL.Append("								b.[dbid]=a.[dbid]")
        sSQL.Append("								and b.[spid]=a.[blocked]")
        sSQL.Append(" where")
        sSQL.Append("	    a.[blocked]!=0")
        sSQL.Append("	    and a.[dbid]=DB_ID() ")

        Blocking_ItemScanStart(lSessionID)

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL.ToString, moDatabase.DecryptedConnectionString)

        For Each oRow As DataRow In oData.Rows
            Dim oBlockLog As New BlockLog
            oBlockLog.DatabaseID = moDatabase.ID
            oBlockLog.SessionID = lSessionID

            oBlockLog.Blocked_SPID = oRow.Item("Blocked_SPID")
            oBlockLog.Blocked_Status = oRow.Item("Blocked_Status")
            oBlockLog.Blocked_Command = oRow.Item("Blocked_Command")
            oBlockLog.Blocked_HostName = oRow.Item("Blocked_HostName")
            oBlockLog.Blocked_LoginName = oRow.Item("Blocked_LoginName")
            oBlockLog.Blocked_Program_Name = oRow.Item("Blocked_Program_Name")

            oBlockLog.Blocking_SPID = oRow.Item("Blocking_SPID")
            oBlockLog.Blocking_Status = oRow.Item("Blocking_Status")
            oBlockLog.Blocking_Command = oRow.Item("Blocking_Command")
            oBlockLog.Blocking_HostName = oRow.Item("Blocking_HostName")
            oBlockLog.Blocking_LoginName = oRow.Item("Blocking_LoginName")
            oBlockLog.Blocking_Program_Name = oRow.Item("Blocking_Program_Name")

            oBlockLog.Save()
        Next

        oData.Dispose()

        Blocking_ItemScanComplete(lSessionID)
    End Sub

    Private Sub RecordConnections(ByVal lSessionID As Long)
        Dim sSQL As String = "select"
        sSQL = sSQL & "   spid ,"
        sSQL = sSQL & "   status,"
        sSQL = sSQL & "   rtrim(loginame) 'loginname',"
        sSQL = sSQL & "   hostname ,"
        sSQL = sSQL & "   convert(char(5),blocked) 'blk',"
        sSQL = sSQL & "   case"
        sSQL = sSQL & "     when dbid = 0 then null"
        sSQL = sSQL & "     when dbid <> 0 then db_name(dbid)"
        sSQL = sSQL & "   end 'dbname',"
        sSQL = sSQL & "   case"
        sSQL = sSQL & "     when dbid=db_id() then 1"
        sSQL = sSQL & "     else 0"
        sSQL = sSQL & "   end 'ConnectedToCurrent',"
        sSQL = sSQL & "   case"
        sSQL = sSQL & "     when spid=@@spid then 1"
        sSQL = sSQL & "     else 0"
        sSQL = sSQL & "   end 'IsHostProcess',"
        sSQL = sSQL & "   cmd"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     master.dbo.sysprocesses"

        Connections_ItemScanStart(moDatabase.ServerID, moDatabase.ID, lSessionID)

        Dim oDBFactory As New Database_Factory
        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL, moDatabase.DecryptedConnectionString)

        Dim oConnectFactory As New SQLAutoDocLib.ConnectLog_Factory
        Dim lServerConnections As Long = 0
        Dim lDatabaseConnections As Long = 0

        For Each oRow As DataRow In oData.Rows
            lServerConnections = lServerConnections + 1
            If oRow.Item("ConnectedToCurrent") = 1 Then lDatabaseConnections = lDatabaseConnections + 1

            Dim oConnectLog As ConnectLog = oConnectFactory.ConnectionIsStillAlive( _
                                                                moDatabase.ID, _
                                                                oRow.Item("spid"), _
                                                                oRow.Item("hostname"), _
                                                                oRow.Item("loginname"), _
                                                                oRow.Item("status"), _
                                                                oRow.Item("blk"), _
                                                                oRow.Item("cmd"))

            If Not oConnectLog Is Nothing Then
                oConnectLog.LastDetectDate = Now
            Else
                oConnectLog = New ConnectLog
                oConnectLog.DatabaseID = moDatabase.ID
                oConnectLog.SessionID = lSessionID

                With oConnectLog
                    .Connect_SPID = oRow.Item("spid")
                    .Connect_Status = oRow.Item("status")
                    .Connect_LoginName = oRow.Item("loginname")
                    .Connect_HostName = oRow.Item("hostname")
                    .Connect_BlockID = oRow.Item("blk")
                    .Connect_Command = oRow.Item("cmd")
                    .Connect_Current = oRow.Item("ConnectedToCurrent")
                    .Connect_IsHostProcess = oRow.Item("IsHostProcess")
                End With
            End If

            oConnectLog.Save()
        Next

        oData.Dispose()

        Dim oSummaryFactory As New ConnectSummary_Factory
        oSummaryFactory.UpdateSummary( _
                            sDate:=Now, _
                            lDatabaseID:=moDatabase.ID, _
                            lSessionCount:=1, _
                            lServerConnections:=lServerConnections, _
                            lDatabaseConnections:=lDatabaseConnections)

        Connections_ItemScanComplete(lSessionID)
    End Sub

    Private Sub RecordWaitStates(ByVal lSessionID As Long)
        WaitStates_ItemScanStart(lSessionID)

        Dim oConfigFactory As New SQLAutoDocLib.Config_Factory
        Dim oConfig As Config = oConfigFactory.GetConfigFromName("WAITSTATE_SCAN_DURATION")

        Dim sSQL As String = "exec ('dbcc SQLPERF(WAITSTATS, CLEAR)')"
        MyBase.ExecuteNonQuery(sSQL, moDatabase.DecryptedConnectionString)

        If Not oConfig Is Nothing Then
            Thread.Sleep(oConfig.Value * 1000)
        End If

        sSQL = "exec ('dbcc SQLPERF(WAITSTATS)')"
        Dim oWaitStates As DataTable = MyBase.ExecuteReadToDataTable(sSQL, moDatabase.DecryptedConnectionString)
        For Each oRow As DataRow In oWaitStates.Rows
            If oRow.Item("Wait Type") <> "Total" _
                 AndAlso (oRow.Item("Wait Time") > 0 OrElse oRow.Item("Requests") > 0 OrElse oRow.Item("Signal Wait Time") > 0) Then

                Dim oWaitState As New WaitStateLog
                With oWaitState
                    .DatabaseID = moDatabase.ID
                    .SessionID = lSessionID

                    .WaitState_WaitType = oRow.Item("Wait Type")
                    .WaitState_Requests = oRow.Item("Requests")
                    .WaitState_Wait_Time = oRow.Item("Wait Time")
                    .WaitState_Signal_Wait_Time = oRow.Item("Signal Wait Time")

                    .Save()
                End With
            End If
        Next
        oWaitStates.Dispose()

        WaitStates_ItemScanComplete(lSessionID)
    End Sub

    Private Sub RecordExtendedProperties(ByVal lSessionID As Long)
        Dim oExtendedPropertiesScanner As New DBScanExtendedProperties
        EXProp_ItemScanStart(lSessionID)
        oExtendedPropertiesScanner.UpdateExtendedProperties(moDatabase.ID)
        EXProp_ItemScanComplete(lSessionID)
    End Sub

    Private Sub RemoveExpiredData(ByVal lSessionID As Long)
        ExpiredData_ItemScanStart(lSessionID)

        Dim oSizeFactory As New TableSize_Factory
        oSizeFactory.RemoveExpiredData()

        Dim oBlockingFactory As New BlockLog_Factory
        oBlockingFactory.RemoveExpiredData()

        Dim oConnectionFactory As New ConnectLog_Factory
        oConnectionFactory.RemoveExpiredData()

        Dim oWaitStateFactory As New WaitStateLog_Factory
        oWaitStateFactory.RemoveExpiredData()

        Dim oScanHistory As New ScanHistory_Factory
        oScanHistory.DeleteExpiredScanHistories()

        ExpiredData_ItemScanComplete(lSessionID)
    End Sub

    Public Sub CheckAndSchedule( _
                ByVal oDatabase As SQLAutoDocLib.Database, _
                ByVal lTypeID As SQLAutoDocLib.ScanSchedule.Type, _
                ByRef OUTPUT_lSessionID As Long)

        Dim oLog As New ErrorLog.Log

        Try
            Dim oIntervalFactory As New SQLAutoDocLib.ScanSchedule_Factory
            Dim oHistoryFactory As New SQLAutoDocLib.ScanQueue_Factory

            Dim lIntervalMinutes As Long = oIntervalFactory.GetIntervalMinutesForDatabase(oDatabase.ID, lTypeID)
            Dim sLastScan As String = oHistoryFactory.GetLastQueuedRequest(oDatabase.ID, lTypeID)

            'oLog.LogMessage("Last scan for " & lTypeID & " for database id " & oDatabase.ID & " occurred at " & sLastScan)

            If (IsDate(sLastScan) AndAlso DateDiff(DateInterval.Minute, CDate(sLastScan), Now) > lIntervalMinutes) _
                    OrElse Not IsDate(sLastScan) Then

                'oLog.LogMessage("New event type " & lTypeID & " for database id " & oDatabase.ID & " because last event occurred at " & sLastScan & " and interval minutes is " & lIntervalMinutes)

                '---No incomplete scan requests currently exists.
                Dim oQueue As SQLAutoDocLib.ScanQueue = oHistoryFactory.GetNextQueuedRequest(oDatabase.ID, lTypeID)
                If oQueue Is Nothing Then
                    If OUTPUT_lSessionID = SQLAutoDocLib.Session.NODATA Then
                        Dim oSession As New SQLAutoDocLib.Session
                        oSession.DatabaseID = oDatabase.ID
                        oSession.Save()
                        OUTPUT_lSessionID = oSession.ID
                    End If

                    '---Schedule new scan
                    oQueue = New SQLAutoDocLib.ScanQueue
                    With oQueue
                        .DatabaseID = oDatabase.ID
                        .ServerID = oDatabase.ServerID
                        .SessionID = OUTPUT_lSessionID
                        .ScanTypeID = lTypeID

                        .Save()
                    End With
                End If
            End If

        Catch oEX As Exception
            oLog.LogMessage(oEX)

            Throw oEX
        End Try
    End Sub

    Private Sub MarkQueueStarted( _
                    ByVal lScanTypeID As ScanSchedule.Type)

        Dim oFactory As New ScanQueue_Factory
        Dim oQueue As ScanQueue = oFactory.GetOpenQueue(moDatabase.ServerID, moDatabase.ID, mlSessionID, lScanTypeID)

        If Not oQueue Is Nothing Then
            oQueue.ScanStart = Now
            oQueue.Save()
        Else
            oQueue = New ScanQueue
            oQueue.ServerID = moDatabase.ServerID
            oQueue.DatabaseID = moDatabase.ID
            oQueue.SessionID = mlSessionID
            oQueue.ScanTypeID = lScanTypeID
            oQueue.ScanStart = Now
            oQueue.Save()
        End If
    End Sub

    Private Sub MarkQueueCompleted( _
                    ByVal lScanTypeID As ScanSchedule.Type)

        Dim oFactory As New ScanQueue_Factory
        Dim oQueue As ScanQueue = oFactory.GetOpenQueue(moDatabase.ServerID, moDatabase.ID, mlSessionID, lScanTypeID)

        If Not oQueue Is Nothing Then
            oQueue.ScanComplete = Now
            oQueue.Save()
        End If
    End Sub

#Region "Event Procedures"
    Public Sub Database_ItemScanStart(ByVal lSessionID As Long)
        MarkQueueStarted(ScanSchedule.Type.SchemaScan)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = moDatabase.ServerID
        oHistory.DatabaseID = moDatabase.ID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.Database"
        oHistory.ObjectID = NODATA
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub Database_ItemScanComplete(ByVal lSessionID As Long)
        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.Database", NODATA)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = Now
            oHistory.Save()
        End If

        MarkQueueCompleted(ScanSchedule.Type.SchemaScan)
    End Sub

    Public Sub Login_ItemScanStart(ByVal lServerID As Long, ByVal lDatabaseID As Long, ByVal lSessionID As Long, ByVal lLoginID As Long)
        Dim oHistory As New ScanHistory
        oHistory.ServerID = lServerID
        oHistory.DatabaseID = lDatabaseID
        oHistory.ObjectType = "SQLAutoDocLib.Login"
        oHistory.ObjectID = lLoginID
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub Login_ItemScanComplete(ByVal lServerID As Long, ByVal lDatabaseID As Long, ByVal lSessionID As Long, ByVal lLoginID As Long)
        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lServerID, lDatabaseID, "SQLAutoDocLib.Login", lLoginID)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = Now
            oHistory.Save()
        End If
    End Sub

    Public Sub Table_ItemScanStart( _
                            ByVal lServerID As Long, _
                            ByVal lDatabaseID As Long, _
                            ByVal lSessionID As Long, _
                            ByVal lTableID As Long)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = lServerID
        oHistory.DatabaseID = lDatabaseID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.Table"
        oHistory.ObjectID = lTableID
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub Table_ItemScanComplete( _
                            ByVal lSessionID As Long, _
                            ByVal lTableID As Long, _
                            ByVal oEndDate As Date)

        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.Table", lTableID)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = oEndDate
            oHistory.Save()
        End If
    End Sub

    Public Sub View_ItemScanStart( _
                        ByVal lServerID As Long, _
                        ByVal lDatabaseID As Long, _
                        ByVal lSessionID As Long, _
                        ByVal lViewID As Long)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = lServerID
        oHistory.DatabaseID = lDatabaseID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.View"
        oHistory.ObjectID = lViewID
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub View_ItemScanComplete( _
                        ByVal lSessionID As Long, _
                        ByVal lViewID As Long, _
                        ByVal oEndDate As Date)

        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.View", lViewID)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = oEndDate
            oHistory.Save()
        End If
    End Sub

    Public Sub SProc_ItemScanStart( _
                        ByVal lServerID As Long, _
                        ByVal lDatabaseID As Long, _
                        ByVal lSessionID As Long, _
                        ByVal lSProcID As Long)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = lServerID
        oHistory.DatabaseID = lDatabaseID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.SProc"
        oHistory.ObjectID = lSProcID
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub SProc_ItemScanComplete( _
                        ByVal lSessionID As Long, _
                        ByVal lSProcID As Long, _
                        ByVal oEndDate As Date)

        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.SProc", lSProcID)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = oEndDate
            oHistory.Save()
        End If
    End Sub

    Public Sub Function_ItemScanStart( _
                            ByVal lServerID As Long, _
                            ByVal lDatabaseID As Long, _
                            ByVal lSessionID As Long, _
                            ByVal lFunctionID As Long)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = lServerID
        oHistory.DatabaseID = lDatabaseID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.Function"
        oHistory.ObjectID = lFunctionID
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub Function_ItemScanComplete( _
                            ByVal lSessionID As Long, _
                            ByVal lFunctionID As Long, _
                            ByVal oEndDate As Date)

        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.Function", lFunctionID)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = oEndDate
            oHistory.Save()
        End If
    End Sub

    Public Sub Blocking_ItemScanStart(ByVal lSessionID As Long)
        MarkQueueStarted(ScanSchedule.Type.BlockScan)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = moDatabase.ServerID
        oHistory.DatabaseID = moDatabase.ID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.BlockLog"
        oHistory.ObjectID = NODATA
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub Blocking_ItemScanComplete(ByVal lSessionID As Long)
        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.BlockLog", NODATA)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = Now
            oHistory.Save()
        End If

        MarkQueueCompleted(ScanSchedule.Type.BlockScan)
    End Sub

    Public Sub Connections_ItemScanStart(ByVal lServerID As Long, ByVal lDatabaseID As Long, ByVal lSessionID As Long)
        MarkQueueStarted(ScanSchedule.Type.ConnectionScan)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = lServerID
        oHistory.DatabaseID = lDatabaseID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.ConnectLog"
        oHistory.ObjectID = NODATA
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub Connections_ItemScanComplete(ByVal lSessionID As Long)
        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.ConnectLog", NODATA)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = Now
            oHistory.Save()
        End If

        MarkQueueCompleted(ScanSchedule.Type.ConnectionScan)
    End Sub

    Public Sub WaitStates_ItemScanStart(ByVal lSessionID As Long)
        MarkQueueStarted(ScanSchedule.Type.WaitStateScan)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = moDatabase.ServerID
        oHistory.DatabaseID = moDatabase.ID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.WaitStateLog"
        oHistory.ObjectID = NODATA
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub WaitStates_ItemScanComplete(ByVal lSessionID As Long)
        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.WaitStateLog", NODATA)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = Now
            oHistory.Save()
        End If

        MarkQueueCompleted(ScanSchedule.Type.WaitStateScan)
    End Sub

    Public Sub Size_ItemScanStart(ByVal lSessionID As Long)
        MarkQueueStarted(ScanSchedule.Type.SizeScan)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = moDatabase.ServerID
        oHistory.DatabaseID = moDatabase.ID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.TableSize"
        oHistory.ObjectID = NODATA
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub Size_ItemScanComplete(ByVal lSessionID As Long)
        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.TableSize", NODATA)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = Now
            oHistory.Save()
        End If

        MarkQueueCompleted(ScanSchedule.Type.SizeScan)
    End Sub

    Public Sub EXProp_ItemScanStart( _
                        ByVal lSessionID As Long)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = moDatabase.ServerID
        oHistory.DatabaseID = moDatabase.ID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.ExtendedProperties"
        oHistory.ObjectID = NODATA
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub EXProp_ItemScanComplete( _
                            ByVal lSessionID As Long)

        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.ExtendedProperties", NODATA)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = Now
            oHistory.Save()
        End If
    End Sub

    Public Sub ExpiredData_ItemScanStart( _
                    ByVal lSessionID As Long)

        Dim oHistory As New ScanHistory
        oHistory.ServerID = moDatabase.ServerID
        oHistory.DatabaseID = moDatabase.ID
        oHistory.SessionID = lSessionID
        oHistory.ObjectType = "SQLAutoDocLib.ExpiredData"
        oHistory.ObjectID = NODATA
        oHistory.ScanStart = Now
        oHistory.Save()
    End Sub

    Public Sub ExpiredData_ItemScanComplete( _
                            ByVal lSessionID As Long)

        Dim oFactory As New ScanHistory_Factory
        Dim oHistory As ScanHistory = oFactory.GetOpenHistory(lSessionID, "SQLAutoDocLib.ExpiredData", NODATA)

        If Not oHistory Is Nothing Then
            oHistory.ScanComplete = Now
            oHistory.Save()
        End If
    End Sub
#End Region

End Class
